﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
using Assist;
using YArchitech.Controls;
using Microsoft.Office.Interop.Excel;
using YJKExcelFormTool.Analyser;
using YJKExcelFormTool.DataDefine;

namespace YJKExcelFormTool.Kits
{
	public class TableExportKit
	{
		public TableExportKit(TableInfos tableInfos, bool usingDefaultFontSetting, TableExportKit.FontSetting fontSetting)
		{
			this.m_TableInfos = tableInfos;
			this.m_EndRowNumb = this.m_TableInfos.Head.TableSourceInfo.BadyEndRowNumber;
			this.m_UsingDefaultFontSetting = usingDefaultFontSetting;
			this.m_FontSetting = fontSetting;
			this.m_StartRowNumb = tableInfos.Head.RowSize;
		}

		public void ExportToExcel()
		{
			if (this.m_TableInfos.Head.HeadType == TableHead._Tag_HeadType.HT_EXCEL && !this.m_TableInfos.Head.TableSourceInfo.IsNull())
			{
				this.ExportToExcelByTemplate();
				return;
			}
			this.ExportToExcelByNullTemplate();
		}

		private void ExportToExcelByNullTemplate()
		{
			this.m_Excel = new ExcelTableAnalyser();
			this.m_Excel.CreateDoc();
			this.ExportTableHead();
			try
			{
				int num = this.m_EndRowNumb - this.m_StartRowNumb;
				if (num <= 0)
				{
					num = this.m_TableInfos.Body.BodyData.Count;
				}
				int num2 = 0;
				int num3 = this.m_TableInfos.Body.BodyData.Count / num;
				if (num3 * num < this.m_TableInfos.Body.BodyData.Count)
				{
					num3++;
				}
				for (int i = 1; i < num3; i++)
				{
					this.m_Excel.NewWorkSheet(0);
				}
				for (int i = 0; i < num3; i++)
				{
					this.m_Excel.SetWorkSheet(i);
					int num4 = i + 1;
					this.m_Excel.SetWorkSheetName("第" + num4.ToString() + "页");
					this.ExportTableBody(num2, num);
					num2 += num;
				}
				this.m_Excel.Save();
			}
			catch (Exception ex)
			{
				this.m_Excel.Close();
				throw ex;
			}
		}

		private bool FileSaveAs(string templatePath, ref string newFilePath)
		{
			SaveFileDialog saveFileDialog = new SaveFileDialog();
			int num = templatePath.LastIndexOf(".");
			if (num < 0)
			{
				return false;
			}
			string text = templatePath.Substring(num + 1, templatePath.Length - num - 1);
			saveFileDialog.Filter = "输出文件(*." + text + ")|*." + text;
			saveFileDialog.RestoreDirectory = true;
			if (saveFileDialog.ShowDialog() == DialogResult.OK)
			{
				newFilePath = saveFileDialog.FileName.ToString();
				File.Copy(templatePath, newFilePath, true);
				FileAttributes fileAttributes = File.GetAttributes(newFilePath);
				fileAttributes &= ~FileAttributes.ReadOnly;
				File.SetAttributes(newFilePath, fileAttributes);
				return true;
			}
			return false;
		}

		private void ExportToExcelByTemplate()
		{
			string name = this.m_TableInfos.Head.TableSourceInfo.Name;
			if (!File.Exists(name))
			{
				YJKMessageBox.Show("模板文件：" + name + "不存在！");
				return;
			}
			string templatePath = "";
			if (!this.FileSaveAs(name, ref templatePath))
			{
				return;
			}
			int sheetIndex = this.m_TableInfos.Head.TableSourceInfo.SheetIndex;
			this.m_Excel = new ExcelTableAnalyser();
			this.m_Excel.CreateDoc(templatePath, sheetIndex);
			try
			{
				int num = this.m_EndRowNumb - this.m_StartRowNumb;
				int num2 = 0;
				int num3 = this.m_TableInfos.Body.BodyData.Count / num;
				if (num3 * num < this.m_TableInfos.Body.BodyData.Count)
				{
					num3++;
				}
				for (int i = 1; i < num3; i++)
				{
					this.m_Excel.NewWorkSheet(sheetIndex);
				}
				for (int i = 0; i < num3; i++)
				{
					this.m_Excel.SetWorkSheet(i);
					int num4 = i + 1;
					this.m_Excel.SetWorkSheetName("第" + num4.ToString() + "页");
					this.ExportTableBody(num2, num);
					num2 += num;
				}
				this.m_Excel.Save();
			}
			catch (Exception ex)
			{
				this.m_Excel.Close();
				throw ex;
			}
		}

		private void ExportTableHead()
		{
			List<double> list = new List<double>();
			for (int i = 0; i < this.m_TableInfos.Head.ColumnSize; i++)
			{
				double num = this.m_TableInfos.Head.GetCellWidthList()[i];
				num = AssistFunc.feetToMM(num / 2.2862);
				list.Add(num);
			}
			List<double> list2 = new List<double>();
			for (int i = 0; i < this.m_TableInfos.Head.RowSize; i++)
			{
				double num2 = this.m_TableInfos.Head.GetCellHeightList()[i];
				num2 = AssistFunc.feetToMM(num2) / 0.3612;
				list2.Add(num2);
			}
			this.m_Excel.SetWidthHeight(list, list2);
			foreach (TableHeadCell tableHeadCell in this.m_TableInfos.Head.TableHeadInfos)
			{
				List<int> rowIndex = tableHeadCell.RowIndex;
				List<int> colIndex = tableHeadCell.ColIndex;
				string key = string.Empty;
				if (tableHeadCell.Key.KeyName.CompareTo("") != 0)
				{
					key = tableHeadCell.Key.KeyName;
				}
				ExcelRange be = new ExcelRange(rowIndex[0] + 1, colIndex[0] + 1, rowIndex.Count, colIndex.Count, tableHeadCell.Value, "YELLOW", true, "BLACK", null, key);
				if (this.m_UsingDefaultFontSetting)
				{
					this.m_Excel.InsertData(be);
				}
				else
				{
					this.m_Excel.InsertData(be, this.m_FontSetting.name, this.m_FontSetting.size, this.HorizontalAlignmentToExcelHAlign(this.m_FontSetting.alignmentH), this.VerticalAlignmentToExcelVAlign(this.m_FontSetting.alignmentV));
				}
			}
		}

		private void ExportTableBody(int curRowOfTable, int rowsCount)
		{
			List<TableHeadCell> effectiveRow = this.m_TableInfos.Head.GetEffectiveRow();
			List<TableBodyRow> bodyData = this.m_TableInfos.Body.BodyData;
			int num = 0;
			while (num < rowsCount && curRowOfTable + num != bodyData.Count)
			{
				int row = this.m_StartRowNumb + num + 1;
				List<TableBodyCell> rowData = bodyData[curRowOfTable + num].RowData;
				for (int i = 0; i < rowData.Count; i++)
				{
					int col = effectiveRow[i].ColIndex[0] + 1;
					if (rowData[i].DataType == TableBodyCell._tagDataType._DT_TEXT)
					{
						ExcelRange be = new ExcelRange(row, col, 1, effectiveRow[i].ColIndex.Count, rowData[i].DataValue, "LightGray", true, "BLACK", null, "");
						if (this.m_UsingDefaultFontSetting)
						{
							this.m_Excel.InsertData(be);
						}
						else
						{
							this.m_Excel.InsertData(be, this.m_FontSetting.name, this.m_FontSetting.size, this.HorizontalAlignmentToExcelHAlign(this.m_FontSetting.alignmentH), this.VerticalAlignmentToExcelVAlign(this.m_FontSetting.alignmentV));
						}
					}
				}
				num++;
			}
		}

		private XlHAlign HorizontalAlignmentToExcelHAlign(TableExportKit._tagHorizontalAlignment tagHAlign)
		{
			if (tagHAlign == TableExportKit._tagHorizontalAlignment.HA_Left)
			{
				return XlHAlign.xlHAlignLeft;
			}
			if (tagHAlign == TableExportKit._tagHorizontalAlignment.HA_Center)
			{
				return XlHAlign.xlHAlignCenter;
			}
			return XlHAlign.xlHAlignRight;
		}

		private XlVAlign VerticalAlignmentToExcelVAlign(TableExportKit._tagVerticalAlignment tagVAlign)
		{
			if (tagVAlign == TableExportKit._tagVerticalAlignment.VA_Top)
			{
				return XlVAlign.xlVAlignTop;
			}
			if (tagVAlign == TableExportKit._tagVerticalAlignment.VA_Center)
			{
				return XlVAlign.xlVAlignCenter;
			}
			return XlVAlign.xlVAlignBottom;
		}

		private ExcelTableAnalyser m_Excel;

		private TableInfos m_TableInfos;

		private int m_StartRowNumb;

		private int m_EndRowNumb;

		private bool m_UsingDefaultFontSetting = true;

		private TableExportKit.FontSetting m_FontSetting;

		public enum _tagHorizontalAlignment
		{
			HA_Left,
			HA_Center,
			HA_Right
		}

		public enum _tagVerticalAlignment
		{
			VA_Top,
			VA_Center,
			VA_Bottom
		}

		public struct FontSetting
		{
			public string name;

			public int size;

			public TableExportKit._tagHorizontalAlignment alignmentH;

			public TableExportKit._tagVerticalAlignment alignmentV;
		}
	}
}
